import { optionCarCategory, optionCarStatus } from './../HuoRestmp.model';
import { Component, OnInit } from '@angular/core';
import {
    FormBuilder,
    FormGroup,
    FormControl,
    Validators
} from '@angular/forms';
import { ApiService } from '../../../../providers/ApiPro.service';
import { NzModalService } from 'ng-zorro-antd';
import { ActivatedRoute , Router, RouterLink, RouterModule} from '@angular/router';

@Component({
  selector: 'app-ResCarDialog',
  templateUrl: './ResCarDialog.component.html',
  styleUrls: ['./ResCarDialog.component.scss']
})
export class ResCarDialogComponent implements OnInit {
 public validateForm: FormGroup;
 public optionCarType: any;
 public optionCompany: any;
 public optionCarCategory = optionCarCategory;
 public optionCarStatus = optionCarStatus;
 public isEdit: boolean = false;
 
  constructor(
    private fb: FormBuilder,
    public apiService: ApiService, 
    private confirmServ: NzModalService,
    private router: Router,
    private route: ActivatedRoute,
    
    
  ) { }

  ngOnInit() {
    this.getId()
    this.getCarType();
    this.getCompany();
  }

  getId(){
    this.initForm();
    
    this.route.params.subscribe((params) => {
           if((params['id'])){
             this.load(params['id']);
             this.isEdit = true;
          }
       });
 }

 initForm(){
    this.validateForm = this.fb.group({
      carId            : [ null, ],
      carNo            : [ null, [ Validators.required ] ],
      isTrailer         : [ null, [ Validators.required ] ],
      carModelId          : [ null, [ Validators.required ] ],
      carStatus        : [ null, [ Validators.required ] ],
      load             : [ null, [ Validators.required ] ],
      carCategory      : [ null, [ Validators.required ] ],
      commercialInsurance          : [ null, [ Validators.required ] ],
      annualInspectionTime          : [ null, [ Validators.required ] ],
      logisticsCompanyId          : [ null, [ Validators.required ] ],
    });
  }

  load(id){
    let url = "car/findinfo"
    this.apiService.get(url, { "carId": id })
      .then(res =>{
        this.validateForm = this.fb.group({
          carId                   : [ res.result.carId, ],
          carNo                   : [ res.result.carNo, [ Validators.required ] ],
          isTrailer               : [ res.result.isTrailer, [ Validators.required ] ],
          carModelId              : [ res.result.carModelId, [ Validators.required ] ],
          carStatus               : [ res.result.carStatus, [ Validators.required ] ],
          load                    : [ res.result.load, [ Validators.required ] ],
          carCategory             : [ res.result.carCategory, [ Validators.required ] ],
          commercialInsurance     : [ res.result.commercialInsurance, [ Validators.required ] ],
          annualInspectionTime    : [ res.result.annualInspectionTime, [ Validators.required ] ],
          logisticsCompanyId      : [ res.result.logisticsCompanyId, [ Validators.required ] ],
        });
        console.log(res.result);
        console.log( this.validateForm.value)
      })
  }

  _submitForm() {
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[ i ].markAsDirty();
      }
    }

  getCarType(){
      let url = "car/findcarmodels";
      this.apiService.get(url)
          .then( res =>{
           this.optionCarType = res.result;
          })
    }

  getCompany(){
    let url ="findlogcoms";
    this.apiService.get(url)
      .then(res =>{
        console.log(res.result)
          this.optionCompany = res.result;
      })
  }

  getFormControl(name) {
    return this.validateForm.controls[ name ];
  }

  createNew(){
    this._submitForm();
    let url = "car/add";
    let urls = "car/update";
    if(this.validateForm.valid){
      this.validateForm.value.annualInspectionTime = this.apiService.formatDateTime2(this.validateForm.value.annualInspectionTime);
     if(this.isEdit){
      this.apiService.post(urls,this.validateForm.value)
      .then(res =>{
        if(res.result ==true){
          this.success();
        }else{
          this.error();
        }
      })
      .catch(err =>{
        this.errorInt();
      })
     }else{
      this.apiService.post(url,this.validateForm.value)
      .then(res =>{
        if(res.result ==true){
          this.success();
        }else{
          this.error();
        }
      })
      .catch(err =>{
        this.errorInt();
      })
     }
    }
  }




  success(){
    let self = this;
      this.confirmServ.success({
      title: '成功',
      content: '车辆信息已完善',
      okText:'完毕',
      onOk() {
          self.router.navigateByUrl("layout/res/carlist");
      },
    });
  }

  error() {
    this.confirmServ.error({
      title: '错误',
      content: '输入的信息有误或以存在，请确认后输入',
      okText:'返回'
    });
  }

  errorInt() {
    this.confirmServ.error({
      title: '错误404',
      content: '网络不稳定，请检查网络设备或与管理员联系',
      okText:'返回'
    });
  }

}
